﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using Navigant.Infrastructure.Helpers;
using Navigant.Infrastructure.UrlDictionary;

namespace Navigant.Infrastructure.Repositories
{
    public class SqlUrlDictionaryRepository : IUrlDictionaryRepository
    {
        #region Private Fields

        private string connectionString;

        #endregion

        #region Constructors

        public SqlUrlDictionaryRepository()
        {
            this.connectionString = ConfigurationManager.ConnectionStrings["NavigantPortal"].ConnectionString;
            Debug.Assert(this.connectionString != string.Empty, "No connection string was provided");
        }

        #endregion

        #region IUrlDictionaryRepository Members

        public string CreateEntry(string value)
        {
            Debug.Assert(value != string.Empty, "No value was provided");

            // First generate a key.
            KeyGenerator random = new KeyGenerator();
            random.Minimum = 4;
            random.Maximum = 8;
            string key = random.GenerateKey();

            using (SqlConnection connection = new SqlConnection(this.connectionString))
            {
                using (SqlCommand command = new SqlCommand("UrlDictionaryEntryCreate",
                           connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@key", key);
                    command.Parameters.AddWithValue("@value", value);
                    command.Parameters.AddWithValue("@newUrl", true);
                    connection.Open();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch(Exception e)
                    {
                        Logger.LogError(e);
                        return null;
                    }
                }
            }
            return key;
        }

        public string RetrieveValue(string key)
        {
            Debug.Assert(key != string.Empty, "No key was provided");
            string value = null;

            using (SqlConnection connection = new SqlConnection(this.connectionString))
            {
                using (SqlCommand command = new SqlCommand("UrlDictionaryEntryGet",
                           connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@key", key);
                    connection.Open();
                    try
                    {
                        value = command.ExecuteScalar().ToString();
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e);
                        return null;
                    }
                }
            }
            return value;
        }

        #endregion
    }
}
